package model;

import java.io.File;
import java.util.ArrayList;

import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.PlaylistEntry;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.data.youtube.YtPublicationState;

import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;



public class YouTubeEntry {

	private PlaylistEntry youTubeVideoEntry;
	private MediaFileSource mediaSourceFile;
	private boolean instertToYouTubeFailed;
	private Integer insertProgress;
	
	public YouTubeEntry(){		
		this.initYouTubeEntry();
	}
	
	public YouTubeEntry(PlaylistEntry youTubeVideoEntry){
		
		this.initYouTubeEntry(youTubeVideoEntry);
	}
	
	public void initYouTubeEntry(){
		this.youTubeVideoEntry = new PlaylistEntry();
		this.instertToYouTubeFailed = false;
		this.insertProgress = 0;
	}
	
	public void initYouTubeEntry(PlaylistEntry youTubeVideoEntry){
		this.youTubeVideoEntry = youTubeVideoEntry;
		this.instertToYouTubeFailed = false;
		this.insertProgress = 0;
	}
	
	public void setVideoTitle(String title){
		
		MediaTitle mediatitle = new MediaTitle();
		mediatitle.setPlainTextContent(title);
		this.youTubeVideoEntry.getOrCreateMediaGroup().setTitle(mediatitle);
	}
	
	public String getVideotitle()
	{
		MediaTitle mediatitle =	this.youTubeVideoEntry.getOrCreateMediaGroup().getTitle();
		return mediatitle.getPlainTextContent();
	}
	
	public void setVideoDescription(String description){
		
		MediaDescription mediaDescription = new MediaDescription();
		mediaDescription.setPlainTextContent(description);
		this.youTubeVideoEntry.getOrCreateMediaGroup().setDescription(mediaDescription);
	}
	
	public String getVideoDescription(){
		MediaDescription mediaDescription = this.youTubeVideoEntry.getOrCreateMediaGroup().getDescription();
		return mediaDescription.getPlainTextContent();
	}
	public void setVideoTags(ArrayList<String> tags){
		
		MediaKeywords mediaKeywords  = new MediaKeywords();
		mediaKeywords.addKeywords(tags);
		this.youTubeVideoEntry.getOrCreateMediaGroup().setKeywords(mediaKeywords);
	
	}
	public ArrayList<String> getVideoTags(){
		MediaKeywords mediaKeywords  = this.youTubeVideoEntry.getOrCreateMediaGroup().getKeywords();
		ArrayList<String> keywordlist = new ArrayList<String>();
		keywordlist.addAll(mediaKeywords.getKeywords());
		return keywordlist;
		
	}
	
	public void addVideoCategory(int category){
		MediaCategory mediaCategory = new MediaCategory();
		mediaCategory.setScheme(YouTubeNamespace.CATEGORY_SCHEME);
		mediaCategory.setContent(Categories.getInstance().getCategory(category).getTerm());
		this.youTubeVideoEntry.getOrCreateMediaGroup().addCategory(mediaCategory);
	}
	
	public ArrayList<MediaCategory> getVideoCategory(){
		ArrayList<MediaCategory> mediaCategories = new ArrayList<MediaCategory>();
		mediaCategories.addAll(this.youTubeVideoEntry.getOrCreateMediaGroup().getCategories());
		return mediaCategories;
	}
	
	public void setVideoPrivacy(boolean isPrivate){
		this.youTubeVideoEntry.getOrCreateMediaGroup().setPrivate(isPrivate);
	}
	
	
	public boolean setMediaSourceFilePath(String filepath){
		File videoFile = new File(filepath);
		if(!videoFile.exists())
		{
			return false;
		}
		
		MimeType mimetype = this.detectMediaType(videoFile);
		this.setMediaSourceFile(new MediaFileSource(videoFile, mimetype.getMediaType()));
		return true;
	}
	
	private MimeType detectMediaType(File videoFile){
		MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.OpendesktopMimeDetector");
		MimeType mimetype = MimeUtil.getMostSpecificMimeType(MimeUtil.getMimeTypes(videoFile));
		return mimetype;	
	}
	
	public boolean isProcessing()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.PROCESSING)
		{
			return true;
		}
		return false;
	}
	
	public boolean isRejected()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.REJECTED)
		{
			return true;
		}
		return false;
	}
	
	public boolean isFailed()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.FAILED)
		{
			return true;
		}
		return false;
	}
	
	public boolean isIncomplete()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.INCOMPLETE)
		{
			return true;
		}
		return false;
	}
	
	public boolean isDeleted()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.DELETED)
		{
			return true;
		}
		return false;
	}
	
	public boolean isRestricted()
	{
		YtPublicationState pubState = youTubeVideoEntry.getPublicationState();
		if(pubState.getState() == YtPublicationState.State.RESTRICTED)
		{
			return true;
		}
		return false;
	}

	private void setMediaSourceFile(MediaFileSource mediaSourceFile) {
		this.mediaSourceFile = mediaSourceFile;
	}

	public MediaFileSource getMediaSourceFile() {
		return mediaSourceFile;
	}

	public PlaylistEntry getYouTubeVideoEntry() {
		return youTubeVideoEntry;
	}

	public void setYouTubeVideoEntry(PlaylistEntry youTubeVideoEntry) {
		this.youTubeVideoEntry = youTubeVideoEntry;
	}

	public boolean isInstertToYouTubeFailed() {
		return instertToYouTubeFailed;
	}

	public void setInstertToYouTubeFailed(boolean uploadFailed) {
		this.instertToYouTubeFailed = uploadFailed;
	}

	public Integer getInsertProgress() {
		return insertProgress;
	}

	public void setInsertProgress(Integer insertProgress) {
		this.insertProgress = insertProgress;
	}

	
}
